package com.ssy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.mapper.InspectMapper;
import com.ssy.system.pojo.CommonType;
import com.ssy.system.pojo.Inspect;
import com.ssy.system.service.ICommonTypeService;
import com.ssy.system.service.IInspectService;
import com.ssy.system.vo.InspectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class InspectServiceImpl extends ServiceImpl<InspectMapper, Inspect> implements IInspectService {

    @Autowired
    private InspectMapper inspectMapper;

    @Autowired
    private ICommonTypeService commonTypeService;

    @Override
    public PageResult loadAllInspectPage(InspectVo inspectVo) {
        Page<Inspect> page = new Page<>(inspectVo.getPage(), inspectVo.getLimit());
        IPage<Inspect> inspects = this.inspectMapper.selectPage(page, new LambdaQueryWrapper<Inspect>()
                .like(StringUtils.hasText(inspectVo.getName()), Inspect::getName, inspectVo.getName())
                .like(StringUtils.hasText(inspectVo.getKeyword()), Inspect::getKeyword, inspectVo.getKeyword())
                .eq(inspectVo.getType() != null, Inspect::getType, inspectVo.getType())
                .eq(inspectVo.getState() != null, Inspect::getState, inspectVo.getState())
                .orderByDesc(Inspect::getCreateTime));

        List<Inspect> inspects1 = inspects.getRecords();

        for (Inspect inspect : inspects1) {
            Integer typeId = inspect.getType();
            CommonType commonType = this.commonTypeService.queryCommonTypeById(typeId);
            inspect.setTypeName(commonType.getName());
        }

        return new PageResult(inspects.getTotal(), inspects1);
    }

    @Override
    public Result insertInspect(Inspect inspect) {
        if (inspect == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.inspectMapper.insert(inspect);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateInspect(Inspect inspect) {
        if (inspect == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.inspectMapper.updateById(inspect);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteInspectById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.inspectMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteInspect(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.inspectMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }
}
